<script type="text/javascript" src="/quizzes/admin/fckeditor/fckeditor.js"></script>
<script type="text/javascript">


    function create_editor_ReplaceTextarea(name){
    var oFCKeditor = new FCKeditor(name);
    oFCKeditor.BasePath = "/quizzes/admin/fckeditor/";
    oFCKeditor.ToolbarSet = "MyToolbar";
    oFCKeditor.Height = "70";
    oFCKeditor.Width = "870";
    oFCKeditor.Config["CustomConfigurationsPath"] = "/quizzes/admin/fckeditor/fckconfig.js";
    oFCKeditor.Config["EditorAreaCSS"] = "/quizzes/admin/fckeditor/editor/css/fckeditor.css";
    oFCKeditor.ReplaceTextarea() ;
    }    
</script>
<?php
	include_once("../Business/survey_service.php");
	include_once("../Business/survey_question_service.php");
	include_once("../Business/survey_answer_service.php");
	include_once("../Business/facebook_app_service.php");
	
	
	include_once("../Business/Data/Entity/survey.php");
	include_once("../Business/Data/Entity/survey_question.php");
	include_once("../Business/Data/Entity/survey_answer.php");
	include_once("../Business/Data/Entity/facebook_app.php");
		
	include_once("uitilities.php");
	include_once("String.php");
		
	$msgstatus = 'display:none';
	$msg = '';
	
	$name = '';
	$questions = '';
	$answers = '';
	$id = get("id");
	
	$s = null;
	if ($_SERVER['REQUEST_METHOD'] == 'POST') {
		if($id == "") {
			$app = new facebook_app(0,'','');
			$app_id = facebook_app_service::insert_facebook_app($app);
			$s = new survey(0, urldecode(post("name")), 0,$app_id);
			$id = survey_service::insert_survey($s);
		}
		else {
			$s = survey_service::get_survey($id);
			$s->survey_name = urldecode(post("name"));
			survey_service::update_survey($s);
		}
		
		
		$nq = post("nq");
		for($i=1 ; $i<=$nq ; $i++) {
			$qid = post("q".$i."id");
			
			
			if($qid == '-1'){
				$sq = new survey_question(0, $id, post("q".$i), 0, 0);
				$sqid = survey_question_service::insert_survey_question($sq);
			}
			else if(strlen($qid)>0 && $qid[0]=='-'){
				$qid = substr($qid, 1);
				survey_question_service::delete_survey_question($qid);
			}
			else{
				$sq = survey_question_service::get_survey_question($qid);
				$sq->survey_question_content = post("q".$i);
				survey_question_service::update_survey_question($sq);
				
				$sqid = $qid;
			}
			
			$na = post("naq".$i);
			for($j=1 ; $j<=$na ; $j++) {

				$aid = post("q".$i."a".$j."id");
				if($aid == '-1'){
					$sa = new survey_answer(0, $sqid, post("q".$i."a".$j), post("q".$i."m".$j));
					survey_answer_service::insert_survey_answer($sa);	
				}
				else if(strlen($aid)>0 && $aid[0]=='-'){
					$aid = substr($aid, 1);
					survey_answer_service::delete_survey_answer($aid);
				}
				else {
					$sa = survey_answer_service::get_survey_answer($aid);
					$sa->survey_answer_content = post("q".$i."a".$j);
					$sa->survey_answer_mark = post("q".$i."m".$j);
					survey_answer_service::update_survey_answer($sa);
				}
			}
		}
		
		$msgstatus = '';
		if(get("id") == "") {
			$id = "";
			$msg = 'Create new survey successfully';
		}
		else {
			$msg = 'Update survey successfully';
		}
	}
	
	if($id != "") {
		$s = survey_service::get_survey($id);
		$name = str_replace('"',"'",$s->survey_name);		
		$sq = survey_question_service::get_survey_question_list_by_survey_id($id);
		$nq = count($sq);
		for($i=1 ; $i<=$nq ; $i++) {
			$q = $sq[$i-1];
			
			$answers = '';
			$sa = survey_answer_service::get_survey_answer_list_by_survey_question_id($q->survey_question_id);
			for($j=1 ; $j<=count($sa) ; $j++) {
				$a = $sa[$j-1];
				$answers = $answers.
				'
				<dl name="a" > 
					<dt><input title="Delete this answer" value="-" type="button" onclick="if(confirmDelete(\'this answer\')) deleteAnswer(this);"> 
						<span id="aTitle">a'.$j.'</span>
						<input title="Mark of answer" id="aMark" type="text" class="mark" name="q'.$i.'m'.$j.'" value="'.$a->survey_answer_mark.'">
						<textarea title="Answer content" id="aContent" type="text" name="q'.$i.'a'.$j.'">'.str_replace('"',"'",$a->survey_answer_content).'</textarea>
						<script type="text/javascript">create_editor_ReplaceTextarea("q'.$i.'a'.$j.'");</script>
						<input type="hidden" name="q'.$i.'a'.$j.'id" id="ida" value="'.$a->survey_answer_id.'" />
					</dt> 
					<dd></dd> 
				</dl> ';
			}

			$questions = $questions.
			'
			<dl name="q" class="question '.($i%2==1 ? 'grey' : '').'"> 
				<dt>
					<input title="Delete this question" value="-" type="button" onclick="if(confirmDelete(\'this question\')) deleteQuestion(this);">  
					<input title="Add an answer" value="+a" type="button" onclick="addAnswer(this)"> 
					<span id="qTitle">q'.$i.'</span> 
					(<span id="nAnswerValue">'.($sa ? count($sa) : '0').'</span>) 
					<textarea title="Question content" id="qContent" type="text" name="q'.$i.'">'.str_replace('"',"'",$q->survey_question_content).'</textarea> 
					<script type="text/javascript">create_editor_ReplaceTextarea("q'.$i.'");</script>
					<input type="hidden" name="q'.$i.'id" id="idq" value="'.$q->survey_question_id.'" />
					<input type="hidden" id="na" name="naq'.$i.'" value="'.($sa ? count($sa) : '0').'" /></dt> 
				<dd class="answer">
				'.$answers.'
				</dd>
			</dl>';			
		}
	}
	else {
		$nq = 0;
	}
	
	

	$main_content = String::Format('
<form method="post">
<center>
<p class="label-submit">
	<input title="Modify result(s)" value="Modify result(s)" type="button" onclick="self.location.href=\'survey_result.php?id='.get("id").'\'">
	<input value="'.(get("id")=='' ? 'Add' : 'Update').' survey" type="submit" title="'.(get("id")=='' ? 'Add' : 'Update').' survey">
	<input title="Add a question" value="+q" type="button" onclick="addQuestion();">
	
</p>
</center>
<h3>Survey</h3>	
    <table class="survey">
        <tbody>
            <tr class="even">
                <td>
                    <dl>
                        <dt>Name</dt>
                    </dl>
                </td>
                <td>
                <textarea type="text" name="name" >'. $name .'</textarea> 
                <script type="text/javascript">
    			create_editor_ReplaceTextarea("name");    
				</script>                
                </td>
            </tr>
        </tbody>
    </table>
<h3>Questions (<span id="nQuestionValue">'.$nq.'</span>)</h3>	
	<input type="hidden" name="nq" value="'.$nq.'" />
	<div id="questions">
	{1}
	</div>
	<center>
	<p class="label-submit">
	
		<input title="Modify result(s)" value="Modify result(s)" type="button" onclick="self.location.href=\'survey_result.php?id='.get("id").'\'">
		<input value="'.(get("id")=='' ? 'Add' : 'Update').' survey" type="submit" title="'.(get("id")=='' ? 'Add' : 'Update').' survey">
		<input title="Add a question" value="+q" type="button" onclick="addQuestion();">
	</p>
	</center>
	
</form>
<script>
	function updateQuestionInfo() {
		$("dl[name=q]").each(function(index) {
			$(this).find("textarea[id=qContent]").attr("name" , "q" + (index + 1));
			create_editor_ReplaceTextarea("q" + (index + 1));
			$(this).find("input[id=idq]").attr("name" , "q" + (index + 1) + "id");
			$(this).find("input[id=na]").attr("name", "naq" + (index + 1) );
		});
		
		$("dl:visible[name=q]").each(function(index) {
			$(this).find("span[id=qTitle]").html("q" + (index + 1));
			
			if(index % 2 == 1) $(this).addClass("grey");
			else $(this).removeClass("grey");
		});
		
		var vcount = $("dl:visible[name=q]").length;
		var count = $("dl[name=q]").length;
		$("#nQuestionValue").html(vcount ? vcount : "0") ;
		$("input[name=nq]").val(count ? count : "0") ;
		
	}
	function updateAnswerInfo(dlNameQ) {
		var qID = dlNameQ.find("textarea[id=qContent]").attr("name");	
		dlNameQ.find("dl[name=a]").each(function(index) {			
			$(this).find("textarea[id=aContent]").attr("name" , qID + "a" + (index + 1));	
			create_editor_ReplaceTextarea(qID + "a" + (index + 1));		
			$(this).find("input[id=ida]").attr("name" , qID + "a" + (index + 1) + "id");
			$(this).find("input[id=aMark]").attr("name" , qID + "m" + (index + 1));
		});

		dlNameQ.find("dl:visible[name=a]").each(function(index) {
			$(this).find("span[id=aTitle]").html("a" + (index + 1));
			
		});
		
		var vcount = dlNameQ.find("dl:visible[name=a]").length;
		var count = dlNameQ.find("dl[name=a]").length;
		
		dlNameQ.find("span[id=nAnswerValue]").html(vcount ? vcount : "0") ;
		dlNameQ.find("input[id=na]").val(count ? count : "0") ;
	}
	
	function addQuestion(){
		$n = $("<dl name=\"q\" class=\"question\" style=\"display:none\"> \
			<dt>\
				<input title=\"Delete this question\" value=\"-\" type=\"button\" onclick=\"if(confirmDelete(\'this question\'))  deleteQuestion(this);\">  \
				<input title=\"Add an answer\" value=\"+a\" type=\"button\" onclick=\"addAnswer(this)\"> \
				<span id=\"qTitle\" /> \
				(<span id=\"nAnswerValue\">0</span>) \
				<textarea title=\"Question content\" id=\"qContent\" type=\"text\"></textarea> \
				<input type=\"hidden\" name=\"\" id=\"idq\" value=\"-1\" /> \
				<input type=\"hidden\" id=\"na\" name=\"naq\" value=\"0\" /></dt> \
			<dd class=\"answers\" />\
		</dl>");
		
		$("#questions").append($n);
		$n.slideDown(600, function(){
			updateQuestionInfo();
		});
		
		
	}
	
	function deleteQuestion(obj){
	
		var dlNameQ = $(obj).parent().parent();
		if(dlNameQ.next().length){
			alert("Please delete last question first!");
			return;
		}
		dlNameQ.slideUp(600,function () {
			var idq = dlNameQ.find("input[id=idq]");
			if(idq.val()=="-1"){
				dlNameQ.remove();
			}
			else {
				var value = idq.val();
				idq.val(-value);
				dlNameQ.hide();
			}
			updateQuestionInfo();
		});
	}
	
	function addAnswer(obj){
		$n = $("<dl name=\"a\" style=\"display:none\"> \
			<dt><input title=\"Delete this answer\" value=\"-\" type=\"button\" onclick=\"if(confirmDelete(\'this answer\'))  deleteAnswer(this);\"> \
				<span id=\"aTitle\" />\
				<input title=\"Mark of answer\" id=\"aMark\" type=\"text\" class=\"mark\" value=\"0\">\
				<textarea title=\"Answer content\" id=\"aContent\" type=\"text\" ></textarea>\
				<input type=\"hidden\" name=\"\" id=\"ida\" value=\"-1\" /> \
			</dt> \
			<dd></dd> \
		</dl>");
		$(obj).parent().next().append($n);
		$n.slideDown(600, function(){
			updateAnswerInfo($(obj).parent().parent());
		});
		
		
	}
	
	function deleteAnswer(obj){
		var dlNameQ = $(obj).parent().parent().parent().parent();
		var dlNameA = $(obj).parent().parent();
		var ida = dlNameA.find("input[id=ida]");
		
		if(dlNameA.next().length){
			alert("Please delete last answer first!");
			return;
		}
		dlNameA.slideUp(600,function () {
			if(ida.val()=="-1"){
				dlNameA.remove();
			}
			else {
				var value = ida.val();
				ida.val(-value);
				
				dlNameA.hide();
				dlNameA.remove();
			}
			updateAnswerInfo(dlNameQ);
		});

	}
</script>
',htmlspecialchars_decode($name), $questions);
	include_once("masters/pages.php");
?> 